<?php
            
use app\model\Record; 
use app\model\Ad; 
use app\model\Comm; 
use app\model\Theme; 
use app\model\Poster; 
use app\model\Worker; 
use app\model\Fans; 


require_once PATH_MODULE .'define.php';
class Index_Page extends PluginMobilePage
{
//获取定位位置
    public function GetShangJiaByLg()
    {
        global $_P, $_G;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $fromuid = $_P['fromuid'];
        $theme = Theme::getThemeById($themeid);
        $user_location = isset($_P['user_location'])?$_P['user_location']:'';


        $shangjia_list = BeilaiModel::getShangjiaList();
        foreach ($shangjia_list as $sk => $sv){
            $shangjia_list[$sk]['address'] = json_decode($sv['address'],true);
            $shangjia_list[$sk]['mappoint'] = json_decode($sv['mappoint'],true);
        }

        $item = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        $other_config = json_decode($item['other_config'],true);
        $school_other_name = $other_config['school_other_name'];
        $course_other_name = $other_config['course_other_name'];
        $gift_other_name = $other_config['gift_other_name'];
        $show_course_flag = $other_config['show_course_flag'];//显示课程选择数 以及统计数据


        $res = [];
        if ($user_location){
            foreach ($shangjia_list as $key => $vv){
                if ($vv['mappoint'] && $vv['address'] && is_array($vv['mappoint'])){
                    $distance_arr = [];
                    foreach ($vv['mappoint'] as $k1 => $v1){
                        $distance_item = BeilaiModel::distance($v1,$user_location);
                        array_push($distance_arr,$distance_item);
                    }
                    $shangjia_list[$key]['min_distance'] = min($distance_arr);
                }
            }
            $sort_filed = array_column($shangjia_list,'min_distance');
            // array_multisort($sort_filed,SORT_ASC,$shangjia_list);
            $shangjia_list = Ad::arraySort($shangjia_list,'min_distance','asc');
            //机构视频
            $sj_list = [];
            //机构详情
            $sj_detail_list = [];

            foreach ($shangjia_list as $key =>$val){
                $sj_list_data = ['name'=>$val['name'],'logo'=>$val['logo'],'topimgs'=>$val['imgs'],
                    'video'=>$val['videoyouku'],
                    'video_back_img'=>$val['video_back_img']?$val['video_back_img']:$val['logo']
                ];
                array_push($sj_list,$sj_list_data);
                //获取机构课程
                $sj_course_list = BeilaiModel::getCourseList($themeid,$val['id'],'',0,0);
                $sj_course_total = BeilaiModel::getCourseTotal($themeid,$val['id']);
                array_push($sj_detail_list,
                    [
                        'name'=>$val['name'],'logo'=>$val['logo'],'topimgs'=>$val['imgs'],
                        'video'=>$val['videoyouku'],
                        'video_back_img'=>$val['video_back_img']?$val['video_back_img']:$val['logo'],
                        'content'=>$val['content'],
                        'address'=>$val['address'],
                        'courses'=>$sj_course_list,
                        'address_total' => count($val['address']),
                        'course_total'=>$sj_course_total,
                        'order_total'=>$val['order_total'],
                        'kefu'=>['avatar'=>$val['logo'],'mobile'=>$val['mobile'],'name'=>$val['name'].'客服']
                    ]);
            }


            $sj_list_str = '';
            $first_item = '';
            //拼接机构视频
            foreach ($sj_list as $index => $item){

                if ($item['logo'] && $item['video']){
                    $sj_list_str .= '<div class="swiper-slide item" onclick="changeVideo('."'".$item['video']."'".','."'". $item['video_back_img']."'".')">'.
                        '<div class="i_box">'.
                        '<div class="img">'.
                        '<img src="'.$item['logo'].'"/>'.
                        '</div>'.
                        '<span>'.$item['name'].'</span>'.
                        '</div></div>';
                    if (empty($first_item)){
                        $first_item['video']=$item['video'];
                        $first_item['video_back_img']=$item['video_back_img'];
                    }
                }
            }
            //机构详情
            $sj_detail_list_str1 = '';
            foreach ($sj_detail_list as $index => $item)
            {
                if ($item['logo'] && $item['video']){
                    $sj_detail_list_str1 .= '<li onclick="showDetail('.$index.')">'.
                                                '<img src="'.$item['logo'].'" alt="">'.
                                                '<div class="right">'.
                                                    '<div class="tit">'.$item['name'].'</div>'.
                                                    '<div class="data"><div class="d_left">'.
                                                            '<span>机构'. $school_other_name .'：'.$item['address_total'].'个</span>'.
                        '<span>机构'. $course_other_name .'：'.$item['course_total'].'门</span>'.
                                                        '</div>';
                    if ($show_course_flag==1){
                        $sj_detail_list_str1 .='<div class="d_right">'.
                        '<span class="dr_t">'.$item['order_total'].'人</span>'.
                        '<span class="dr_b"><img src="'.$_G['URL']['BEILAI_IMG'].'/peo.png"/>已报名</span>'.
                        '</div>';
                    }
                    $sj_detail_list_str1 .= '</div>'.
                        '</div></li>';
                }
            }

            //机构详情 弹出详情
            $sj_detail_list_str2 = '';
            foreach ($sj_detail_list as $index => $item)
            {
                $item_address_str = '';
                if (count($item['address'])>0){
                    foreach ($item['address'] as $address){
                        $item_address_str.= '<p>'.$address.'</p>';
                    }
                }
                $item_course_str = '';
                foreach ($item['courses'] as $course){
                    $item_course_str.= '<li><img class="left" src="'.$course['top_img'].'"><div class="right"><span>'.$course['name'].
                        '</span>';
                    if ($show_course_flag==1){
                        $item_course_str.= '<span class="bot"><img class="tel" src="'.$_G['URL']['BEILAI_IMG'].'/peo.png"/>已报名<i>'.$course['selected_num'].'人</i></span>';
                    }
                    $item_course_str.= '</li>';
                }
                $sj_detail_list_str2 .= '<div class="org_detail">'.
                        '<div class="org_item">'.
                            '<div class="org_title">'.$item['name'].'<a href="javascript:hideDetail()" class="close"><img src="'.$_G['URL']['BEILAI_IMG'].'/close.png"></a></div>'.
                            '<div class="org_cont">'.
                                '<div class="serve" style="display: none;">'.
                                    '<div class="name">'.
                                        '<img class="head" src="'.$item['kefu']['avatar'].'">'.
                                        '<div class="txt">'.
                                            '<span>'.$item['kefu']['name'].'</span>'.
                                            '<span class="tel"><img src="'.$_G['URL']['BEILAI_IMG'].'/tel.png">'.$item['kefu']['mobile'].'</span>'.
                                        '</div>'.
                                    '</div>'.
                                    '<img class="phone" src="'.$_G['URL']['BEILAI_IMG'].'/phone.png">'.
                                '</div>'.
                                '<div class="detail_txt">'.
                                    '<div class="dt_txt">机构介绍</div>'.
                                    '<div class="db_txt">'.$item['content'].'</div>'.
                                '</div>'.
                                '<div class="address">'.
                                    '<div class="dt_txt">机构'. $school_other_name .'</div>'.
                                    '<div class="db_txt">'.$item_address_str.
                                    '</div>'.
                                '</div>'.
                                '<div class="course">'.
                                    '<div class="dt_txt">机构'. $course_other_name .'</div>'.
                                    '<ul class="list">
                                        '.$item_course_str.'</ul></div></div></div></div>';
            }

            $res = [
                'first_url'=>$first_item['video'],
                'first_poster'=>$first_item['video_back_img'],
                'sj_list_str'=>$sj_list_str,
                'sj_detail_list_str1'=>$sj_detail_list_str1,
                'sj_detail_list_str2'=>$sj_detail_list_str2,
            ];
        }

        return_json(0,'',$res);
    }
//我的
    public function Mine()
    {
        global $_P, $_G,$_S;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $fromuid = $_P['fromuid'];
        $theme = Theme::getThemeById($themeid);
        $_C = $config =    Theme::getConfig($themeid);
        $system = $_S;
        $kefu = ['qr_code'=>$config['kfewm'],'mobile'=>$theme['mobile']];
        $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
        //海报地址

        $poster = Poster::getByThemeIdnew($themeid);
        if(!empty($poster['bg'])){
            $config['isposter']=1;
            if ($fans['status']==2 && $_S['s']['istubiao']==1) {
                $config['isposter']=0;
            }
            $pm = ['themeid'=>$themeid,'openid'=>$fans['openid']];
            $myposter = db_find("SELECT * FROM ".tablename(t_fans)."
         WHERE themeid = :themeid and openid = :openid",$pm);
            if (empty($myposter['poster'])){
                $waittips = empty($poster['waittips'])?'正在生成海报':$poster['waittips'];
            }else{
                $waittips='';
            }
            //获取海报的url地址
            $posterurl = "/" . createMobileUrl("poster",array("themeid"=>$themeid,"op"=>"action","fromuid"=>$_P['fromuid'],"fopenid"=>$_P['fopenid'],"recordid"=>intval($themyrecord['id'])));

        }else{
            $waittips='海报未开启';
        }
        $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid);
        $company_info = [];
        if ($myworker['company']){
            $company_info = BeilaiModel::getShangjiaItem($myworker['company']);
        }
        $item = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        $poster_show = $item['poster_show'];
        //伪小程序
        if ($system['pn_wxapp']==3){
            $poster_show==2;
        }

        $setting = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        $other_config = json_decode($setting['other_config'],true);
        $school_other_name = $other_config['school_other_name'];
        $course_other_name = $other_config['course_other_name'];
        $gift_other_name = $other_config['gift_other_name'];


        $canshare =1;
        if ($_S['softstyle']==108){
            if (!$themyrecord && !$myworker && $poster_show==2){
                $posterurl = '';
                $waittips = '成功购买'.$course_other_name.'后，才能生成海报';
                $canshare=0;
            }
        }elseif ($_S['softstyle']=108 && $_S['beilai_style']==1){
            if (!$themyrecord && !$myworker && $poster_show==2){
                $posterurl = '';
                $waittips = '成功购买'.$course_other_name.'后，才能生成海报';
                $canshare=0;
            }
        }
        if ($system['pn_wxapp']==1){
            $canshare = 0;
            $posterurl = '';
            $waittips = '未开启分享功能';
        }

        //邀请信息
        $invite_info = [];
        $user_share_num = $fans['responsum'];
        //排除 自己团成员记录   更新推荐人数 包含团内成员
        $tuijian_list = db_select("SELECT * FROM ".tablename(t_record)."
	                            WHERE  themeid =" . $themeid ." and status > 0  and fromuid = " .$_G['member']['uid'] ." order by id desc");
        $user_tuijian_num = count($tuijian_list);
        $poster_info = ['tips'=>$waittips,'img_url'=>$posterurl];
        //底部图
        $setting = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        //总提现金额
        $pm = array(":themeid"=>$themeid,":openid"=>$_G['openid']);
        $totalsuccess = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
                WHERE m.themeid = :themeid and (status=1 or status=98 and type=99) and m.openid = :openid',$pm);
        $commssum = Comm::getcommssum($_S, $themeid, $config['iscommshowfail']);
        $return_clicent = get_defined_vars();
return $this->view('/beilai/mine', $return_clicent);
    }
//  预订单确认
    public function PayConfirm()
    {
        global $_P, $_G,$_S;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $fromuid = $_P['fromuid'];
        $theme = Theme::getThemeById($themeid);
        $workerid = $_P['workerid'];
        $_C = $config =    Theme::getConfig($themeid);
        $joinid = isset($_P['joinid'])?$_P['joinid']:0;
        $recordid = isset($_P['recordid'])?$_P['recordid']:0;
        $grouprice = $_P['grouprice']?$_P['grouprice']:0;
        if ($_G['ispost']){
            $course_address = $_P['course_address'];
            $gift_ids = explode(",",$_P['gift_ids']);
            foreach ($course_address as $key => $val)
            {
                $course_ids .= $val['courseid'].',';
            }
            $course_ids = rtrim($course_ids,',');

            $checked_course_list = BeilaiModel::getCourseListByIds($themeid,$course_ids);

            //插入礼品、选择课程

            $format_data = [
                'checked_course_list'=>$checked_course_list
            ];
            return_json(0,'',$format_data);
        }else{
            $form = [
                ['title'=>$config['nametext'],'type'=>'txt','name'=>'nametext','val'=>'','placeholder'=>'请输入'.$config['nametext']],
                ['title'=>$config['mobiletext'],'type'=>'txt','name'=>'mobiletext','val'=>'','placeholder'=>'请输入'.$config['mobiletext']],
            ];
            if ($config['datetime']){
                $type = 'txt';
                switch ($config['datetimetype']){
                    case '2':
                        $type = 'number';
                        break;
                    case '6':
                        $type = 'mobile';
                        break;
                    case '0':
                        $type = 'date';
                        break;
                    default:
                        break;
                }
                $temp_arr = ['title'=>$config['datetime'],'type'=>$type,'name'=>'datetime','val'=>'','placeholder'=>'请输入'.$config['datetime']];
                array_push($form,$temp_arr);
            }
            if ($config['address']){
                $type = 'txt';
                switch ($config['addresstype']){
                    case '1':
                        $type = 'number';
                        break;
                    case '6':
                        $type = 'mobile';
                        break;
                    case '7':
                        $type = 'date';
                        break;
                    case '2':
                        $type = 'select';
                        break;
                    case '3':
                        $type = 'radio';
                        break;
                    default:
                        break;
                }
                $temp_arr = ['title'=>$config['address'],'type'=>$type,'name'=>'address','val'=>$config['addressoptions'],'placeholder'=>'请输入'.$config['address']];
                array_push($form,$temp_arr);
            }
            if ($config['formtwo']){
                $type = 'txt';
                switch ($config['formtwotype']){
                    case '1':
                        $type = 'number';
                        break;
                    case '6':
                        $type = 'mobile';
                        break;
                    case '7':
                        $type = 'date';
                        break;
                    case '2':
                        $type = 'select';
                        break;
                    case '3':
                        $type = 'radio';
                        break;
                    default:
                        break;
                }
                $temp_arr = ['title'=>$config['formtwo'],'type'=>$type,'name'=>'formtwo','val'=>$config['formtwooptions'],'placeholder'=>'请输入'.$config['formtwo']];
                array_push($form,$temp_arr);
            }
            if ($config['addinfo']){
                $type = 'txt';
                switch ($config['addinfotype']){
                    case '2':
                        $type = 'number';
                        break;
                    case '6':
                        $type = 'mobile';
                        break;
                    case '0':
                        $type = 'date';
                        break;
                    default:
                        break;
                }
                $temp_arr = ['title'=>$config['addinfo'],'type'=>$type,'name'=>'addinfo','val'=>'','placeholder'=>'请输入'.$config['addinfo']];
                array_push($form,$temp_arr);
            }
            if ($config['shops']){
                $type = 'txt';
                $val = '';
                switch ($config['shopstype']){
                    case '2':
                        $type = 'number';
                        break;
                    case '6':
                        $type = 'mobile';
                        break;
                    case '7':
                        $type = 'date';
                        break;
                    case '0':
                        $type = 'select';
                        $range_val = $config['options'];
                        if (!$config['gudingoptions']){
                            $val = $range_val;
                        }else{
                            $guding_val = $config['gudingoptions'];
                            $val = array_merge($guding_val,$range_val);
                            $limit_config = ['min'=>$config['shopsmin'],'max'=>$config['shopsmax']];
                        }
                        break;
                    case '3':
                        $type = 'radio';
                        $val = $config['options'];
                        break;
                    case '4':
                        $type = 'checkbox';
                        $range_val = $config['options'];
                        if (!$config['gudingoptions']){
                            $val = $range_val;
                        }else{
                            $guding_val = $config['gudingoptions'];
                            $val = array_merge($guding_val,$range_val);
                            $limit_config = ['min'=>$config['shopsmin'],'max'=>$config['shopsmax']];
                        }
                        break;
                    case '5':
                        $type = 'shop_checkbox';
                        break;
                    default:
                        break;
                }
                $temp_arr = ['title'=>$config['shops'],'type'=>$type,'name'=>'shops','val'=>$val,'placeholder'=>'请输入'.$config['shops']];
                array_push($form,$temp_arr);
            }
//            启用多价格
            if ($_S['bottom_jiage']>0){
                if ($joinid==4 || $grouprice==2){
//                    拼团价格2
                    $form = [
                        ['title'=>$config['form2']['nametext'],'type'=>'txt','name'=>'nametext','val'=>'','placeholder'=>'请输入'.$config['form2']['nametext']],
                        ['title'=>$config['form2']['mobiletext'],'type'=>'txt','name'=>'mobiletext','val'=>'','placeholder'=>'请输入'.$config['form2']['mobiletext']],
                    ];
                    if ($config['form2']['datetime']){
                        $type = 'txt';
                        switch ($config['form2']['datetimetype']){
                            case '2':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '0':
                                $type = 'date';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form2']['datetime'],'type'=>$type,'name'=>'datetime','val'=>'','placeholder'=>'请输入'.$config['form2']['datetime']];
                        array_push($form,$temp_arr);
                    }
                    if ($config['form2']['address']){
                        $type = 'txt';
                        switch ($config['form2']['addresstype']){
                            case '1':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '7':
                                $type = 'date';
                                break;
                            case '2':
                                $type = 'select';
                                break;
                            case '3':
                                $type = 'radio';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form2']['address'],'type'=>$type,'name'=>'address','val'=>$config['form2']['addressoptions'],'placeholder'=>'请输入'.$config['form2']['address']];
                        array_push($form,$temp_arr);
                    }
                    if ($config['form2']['formtwo']){
                        $type = 'txt';
                        switch ($config['form2']['formtwotype']){
                            case '1':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '7':
                                $type = 'date';
                                break;
                            case '2':
                                $type = 'select';
                                break;
                            case '3':
                                $type = 'radio';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form2']['formtwo'],'type'=>$type,'name'=>'formtwo','val'=>$config['form2']['formtwooptions'],'placeholder'=>'请输入'.$config['form2']['formtwo']];
                        array_push($form,$temp_arr);
                    }
                    if ($config['form2']['addinfo']){
                        $type = 'txt';
                        switch ($config['form2']['addinfotype']){
                            case '2':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '0':
                                $type = 'date';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form2']['addinfo'],'type'=>$type,'name'=>'addinfo','val'=>'','placeholder'=>'请输入'.$config['form2']['addinfo']];
                        array_push($form,$temp_arr);
                    }
                    if ($config['form2']['shops']){
                        $type = 'txt';
                        $val = '';
                        switch ($config['form2']['shopstype']){
                            case '2':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '7':
                                $type = 'date';
                                break;
                            case '0':
                                $type = 'select';
                                $range_val = $config['form2']['options'];
                                if (!$config['form2']['gudingoptions']){
                                    $val = $range_val;
                                }else{
                                    $guding_val = $config['form2']['gudingoptions'];
                                    $val = array_merge($guding_val,$range_val);
                                    $limit_config = ['min'=>$config['form2']['shopsmin'],'max'=>$config['form2']['shopsmax']];
                                }
                                break;
                            case '3':
                                $type = 'radio';
                                $val = $config['form2']['options'];
                                break;
                            case '4':
                                $type = 'checkbox';
                                $range_val = $config['form2']['options'];
                                if (!$config['form2']['gudingoptions']){
                                    $val = $range_val;
                                }else{
                                    $guding_val = $config['form2']['gudingoptions'];
                                    $val = array_merge($guding_val,$range_val);
                                    $limit_config = ['min'=>$config['form2']['shopsmin'],'max'=>$config['form2']['shopsmax']];
                                }
                                break;
                            case '5':
                                $type = 'shop_checkbox';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form2']['shops'],'type'=>$type,'name'=>'shops','val'=>$val,'placeholder'=>'请输入'.$config['form2']['shops']];
                        array_push($form,$temp_arr);
                    }
                    $config['nametext'] = $config['form2']['nametext'];
                    $config['mobiletext'] = $config['form2']['mobiletext'];
                    $config['datetime'] = $config['form2']['datetime'];
                    $config['datetimetype'] = $config['form2']['datetimetype'];
                    $config['address'] = $config['form2']['address'];
                    $config['addresstype'] = $config['form2']['addresstype'];
                    $config['addressoptions'] = $config['form2']['addressoptions'];
                    $config['formtwo'] = $config['form2']['formtwo'];
                    $config['formtwotype'] = $config['form2']['formtwotype'];
                    $config['formtwooptions'] = $config['form2']['formtwooptions'];
                    $config['addinfo'] = $config['form2']['addinfo'];
                    $config['addinfotype'] = $config['form2']['addinfotype'];
                    $config['shops'] = $config['form2']['shops'];
                    $config['shopstype'] = $config['form2']['shopstype'];
                    $config['options'] = $config['form2']['options'];
                    $config['gudingoptions'] = $config['form2']['gudingoptions'];
                    $config['shopsmin'] = $config['form2']['shopsmin'];
                    $config['shopsmax'] = $config['form2']['shopsmax'];
                }elseif ($joinid==5 || $grouprice==3){
//                    拼团价格3
                    $form = [
                        ['title'=>$config['form3']['nametext'],'type'=>'txt','name'=>'nametext','val'=>'','placeholder'=>'请输入'.$config['form3']['nametext']],
                        ['title'=>$config['form3']['mobiletext'],'type'=>'txt','name'=>'mobiletext','val'=>'','placeholder'=>'请输入'.$config['form3']['mobiletext']],
                    ];
                    if ($config['form3']['datetime']){
                        $type = 'txt';
                        switch ($config['form3']['datetimetype']){
                            case '2':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '0':
                                $type = 'date';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form3']['datetime'],'type'=>$type,'name'=>'datetime','val'=>'','placeholder'=>'请输入'.$config['form3']['datetime']];
                        array_push($form,$temp_arr);
                    }
                    if ($config['form3']['address']){
                        $type = 'txt';
                        switch ($config['form3']['addresstype']){
                            case '1':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '7':
                                $type = 'date';
                                break;
                            case '2':
                                $type = 'select';
                                break;
                            case '3':
                                $type = 'radio';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form3']['address'],'type'=>$type,'name'=>'address','val'=>$config['form3']['addressoptions'],'placeholder'=>'请输入'.$config['form3']['address']];
                        array_push($form,$temp_arr);
                    }
                    if ($config['form3']['formtwo']){
                        $type = 'txt';
                        switch ($config['form3']['formtwotype']){
                            case '1':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '7':
                                $type = 'date';
                                break;
                            case '2':
                                $type = 'select';
                                break;
                            case '3':
                                $type = 'radio';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form3']['formtwo'],'type'=>$type,'name'=>'formtwo','val'=>$config['form3']['formtwooptions'],'placeholder'=>'请输入'.$config['form3']['formtwo']];
                        array_push($form,$temp_arr);
                    }
                    if ($config['form3']['addinfo']){
                        $type = 'txt';
                        switch ($config['form3']['addinfotype']){
                            case '2':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '0':
                                $type = 'date';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form3']['addinfo'],'type'=>$type,'name'=>'addinfo','val'=>'','placeholder'=>'请输入'.$config['form3']['addinfo']];
                        array_push($form,$temp_arr);
                    }
                    if ($config['form3']['shops']){
                        $type = 'txt';
                        $val = '';
                        switch ($config['form3']['shopstype']){
                            case '2':
                                $type = 'number';
                                break;
                            case '6':
                                $type = 'mobile';
                                break;
                            case '7':
                                $type = 'date';
                                break;
                            case '0':
                                $type = 'select';
                                $range_val = $config['form3']['options'];
                                if (!$config['form3']['gudingoptions']){
                                    $val = $range_val;
                                }else{
                                    $guding_val = $config['form3']['gudingoptions'];
                                    $val = array_merge($guding_val,$range_val);
                                    $limit_config = ['min'=>$config['form3']['shopsmin'],'max'=>$config['form3']['shopsmax']];
                                }
                                break;
                            case '3':
                                $type = 'radio';
                                $val = $config['form3']['options'];
                                break;
                            case '4':
                                $type = 'checkbox';
                                $range_val = $config['form3']['options'];
                                if (!$config['form3']['gudingoptions']){
                                    $val = $range_val;
                                }else{
                                    $guding_val = $config['form3']['gudingoptions'];
                                    $val = array_merge($guding_val,$range_val);
                                    $limit_config = ['min'=>$config['form3']['shopsmin'],'max'=>$config['form3']['shopsmax']];
                                }
                                break;
                            case '5':
                                $type = 'shop_checkbox';
                                break;
                            default:
                                break;
                        }
                        $temp_arr = ['title'=>$config['form3']['shops'],'type'=>$type,'name'=>'shops','val'=>$val,'placeholder'=>'请输入'.$config['form3']['shops']];
                        array_push($form,$temp_arr);
                    }
                    $config['nametext'] = $config['form3']['nametext'];
                    $config['mobiletext'] = $config['form3']['mobiletext'];
                    $config['datetime'] = $config['form3']['datetime'];
                    $config['datetimetype'] = $config['form3']['datetimetype'];
                    $config['address'] = $config['form3']['address'];
                    $config['addresstype'] = $config['form3']['addresstype'];
                    $config['addressoptions'] = $config['form3']['addressoptions'];
                    $config['formtwo'] = $config['form3']['formtwo'];
                    $config['formtwotype'] = $config['form3']['formtwotype'];
                    $config['formtwooptions'] = $config['form3']['formtwooptions'];
                    $config['addinfo'] = $config['form3']['addinfo'];
                    $config['addinfotype'] = $config['form3']['addinfotype'];
                    $config['shops'] = $config['form3']['shops'];
                    $config['shopstype'] = $config['form3']['shopstype'];
                    $config['options'] = $config['form3']['options'];
                    $config['gudingoptions'] = $config['form3']['gudingoptions'];
                    $config['shopsmin'] = $config['form3']['shopsmin'];
                    $config['shopsmax'] = $config['form3']['shopsmax'];
                }
            }
            //获取用户优惠券
            $coupon_item = BeilaiModel::getUserCouponInfo($themeid,$uid);
            $theme_status = BeilaiModel::checkThemeStatus($theme);
            if ($joinid==9){
                $price = $old_price = $config['primeprice'];
                if ($coupon_item){
                    if ((float)$coupon_item['price']<(float)$old_price){
                        $price = ($old_price-$coupon_item['price']);
                    }else{
                        $coupon_item['price'] = '';
                    }
                    $price_info = ['now_price'=>$price,'coupon'=>$coupon_item['price']];
                }else{
                    $price_info = ['now_price'=>$price,'coupon'=>''];
                }
            }else{
                $price = $old_price = $config['groupprice'];
//                启用多价格
                if ($_S['bottom_jiage']>0){
                    if ($joinid==4 || $grouprice==2){
                        $price = $old_price = $config['groupprice2'];
                    }elseif ($joinid==5 || $grouprice==3){
                        $price = $old_price = $config['groupprice3'];
                    }
                }
                if ($coupon_item){
                    //判断优惠券绑定
                    if ((float)$coupon_item['price']<(float)$old_price){
                        $price = bcsub($old_price,$coupon_item['price'],2);
                    }else{
                        $coupon_item['price'] = '';
                    }
                    $price_info = ['now_price'=>$price,'coupon'=>$coupon_item['price']];
                }else{
                    $price_info = ['now_price'=>$price,'coupon'=>''];
                }
            }
            $setting = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
            $other_config = json_decode($setting['other_config'],true);
            $school_other_name = $other_config['school_other_name'];
            $course_other_name = $other_config['course_other_name'];
            $gift_other_name = $other_config['gift_other_name'];
            //员工身份以及是否可以购买判断
            $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid);
        }
        $register_jssdk = $this->getJsSdkConfig();
        $return_clicent = get_defined_vars();
        return $this->view('/beilai/pay_confirm', $return_clicent);
    }
//  关于我们
    public function About()
    {
        global $_P, $_G,$_S;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $fromuid = $_P['fromuid'];
        $theme = Theme::getThemeById($themeid);
        $_C = $config =    Theme::getConfig($themeid);
        $op = isset($_P['op'])?$_P['op']:'display';

        if ($op=='display'){
            $where = ['uid'=>$fans['uid'],'themeid'=>$themeid];
            require_once get_path('plugin/beilai/core/model.php');
            $has_flag = BeilaiModel::getCompanyInfoByWhere($where);
            $return_clicent = get_defined_vars();
return $this->view('/beilai/about', $return_clicent);
        }else{
            $mobile = isset($_P['mobile'])?$_P['mobile']:'';
            $username = isset($_P['username'])?$_P['username']:'';
            $company_name = isset($_P['company_name'])?$_P['company_name']:'';
            $address = isset($_P['address'])?$_P['address']:'';
            $type = 2;
            if ($company_name){
                $type=1;
            }
            $insert_data = [
                'uid'=>$uid,
                'mobile'=>$mobile,
                'username'=>$username,
                'company_name'=>$company_name,
                'address'=>$address,
                'type'=>$type,
                'themeid'=>$themeid,
                'appid'=>$_G['platform']['appid'],
            ];
            require_once get_path('plugin/beilai/core/model.php');
            $res = BeilaiModel::insertCompanyInfo($insert_data);

            if (!$res){
                return_json(1,'保存失败，请重试',[]);
            }else{
                return_json(0,'',[]);
            }
        }

    }
//  赠品选择
    public function Gift()
    {
        global $_P, $_G,$_S;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $fromuid = $_P['fromuid'];
        $type = $_P['type'];
        $joinid = isset($_P['joinid'])?$_P['joinid']:0;
        $recordid = isset($_P['recordid'])?$_P['recordid']:0;
        $grouprice = $_P['grouprice']?$_P['grouprice']:0;
        $theme = Theme::getThemeById($themeid);
        $_C = $config =  Theme::getConfig($themeid);

        //免费礼品
        $free = BeilaiModel::getGiftList($themeid,0,'',0,0,0,1);
        //获取可选礼品数量
        $setting = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        $other_config = json_decode($setting['other_config'],true);
        $school_other_name = $other_config['school_other_name'];
        $course_other_name = $other_config['course_other_name'];
        $gift_other_name = $other_config['gift_other_name'];

        $gift_min_num = $setting['gift_min_num'];
        $gift_max_num = $setting['gift_max_num'];
        if ($_S['bottom_jiage']>0){
            if ($joinid==4 || $grouprice==2){
                $gift_min_num = $setting['p2_gift_min_num'];
                $gift_max_num = $setting['p2_gift_max_num'];
            }elseif ($joinid==5 || $grouprice==3){
                $gift_min_num = $setting['p3_gift_min_num'];
                $gift_max_num = $setting['p3_gift_max_num'];
            }
        }
        $return_clicent = get_defined_vars();
return $this->view('/beilai/gift', $return_clicent);
    }
//    课程选择
    public function Course()
    {
        global $_P, $_G,$_S;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $theme = Theme::getThemeById($themeid);
        $grouprice = $_P['grouprice']?$_P['grouprice']:0;
        if ($fans['nickname']=='微信用户' || $fans['nickname']=='新用户'){
            $workerurl = Fans::get_login_url($_S,$theme);
            //如果员工打开的链接为原始链接则先刷新
            echo "<script language='javascript' type='text/javascript'>";
            echo "window.location.href='$workerurl';";
            echo "</script>";
            exit;
        }
        $fromuid = $_P['fromuid'];
        $_C = $config =    Theme::getConfig($themeid);
        $joinid = isset($_P['joinid'])?$_P['joinid']:0;
        $recordid = isset($_P['recordid'])?$_P['recordid']:0;
        $user_location = isset($_P['user_location'])?$_P['user_location']:'';
        //获取可选课程数量
        $setting = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        $open_shangjia_rules = 0;
        if ($_S['beilai_style']==1){
            $open_shangjia_rules = $setting['open_shangjia_rules'];
        }

        $course_min_num = $setting['course_min_num'];
        $course_max_num = $setting['course_max_num'];

        $other_config = json_decode($setting['other_config'],true);
        $school_other_name = $other_config['school_other_name'];
        $course_other_name = $other_config['course_other_name'];
        $gift_other_name = $other_config['gift_other_name'];
        $course_mode = $other_config['course_mode'];
        $shangjia_select_max = (int)$other_config['shangjia_select_max'];

        $sec_bk_c1 = $other_config['sec_bk_c1'];
        $sec_bk_c2 = $other_config['sec_bk_c2'];
        $sec_txt_c = $other_config['sec_txt_c'];
        $sec_txt_size = $other_config['sec_txt_size'];
        $show_course_flag = $other_config['show_course_flag'];//显示课程选择数 以及统计数据



        if ($open_shangjia_rules==1){
            //获取全部的机构类型
            $shangjia_types = json_decode($setting['shangjia_rules'],true);
            $temp_shangjia_types_arr = [];
            foreach ($shangjia_types as $kk => $vv){
                $temp_arr = ['id'=>$vv['shangjia_rule_id'],'title'=>$vv['rule_content'],'is_special'=>$vv['rule_special'],'rule_sort'=>$vv['rule_sort']];
                array_push($temp_shangjia_types_arr,$temp_arr);
            }
            //排序
            $sort_filed = array_column($temp_shangjia_types_arr,'rule_sort');
            array_multisort($sort_filed,SORT_ASC,$temp_shangjia_types_arr);

            //二级分类
            //二级分类下设定数组
            $ct_shangjia_list = [];
            //获取一级主分类
            $first_list = BeilaiModel::getCategoryListBy(['themeid'=>$themeid,'pid'=>0]);
            foreach ($first_list as $e => $l){
                $temp_arr = ['id'=>$l['ct_id'],'title'=>$l['ct_name'],
                    'is_special'=>($l['ct_sj']==0 && $l['ct_sj_min']==0)?1:0,'rule_sort'=>$l['ct_sort'],'has_child'=>0,'list_info'=>[],'ct_sj'=>$l['ct_sj'],'ct_sj_min'=>$l['ct_sj_min']];
                array_push($ct_shangjia_list,$temp_arr);
            }
            //排序
            $sort_filed = array_column($ct_shangjia_list,'rule_sort');
            array_multisort($sort_filed,SORT_ASC,$ct_shangjia_list);
        }



        //获取全部可选课程
        $course_list = BeilaiModel::getCourseList($themeid,0,'',0,0,1);
        //多价格处理
        if ($_S['bottom_jiage']>0){
            $show_shangjia_arr = [];
            $group_sj = BeilaiModel::getJoinGroupPriceSj($joinid);
            foreach ($course_list as $key => $val)
            {
                if (in_array($val['shangjia_id'],$group_sj)){
                    $course_list[$key]['address'] = json_decode($val['address'],true);
                    $course_list[$key]['mappoint'] = json_decode($val['mappoint'],true);
                    if (!in_array($val['shangjia_id'],$show_shangjia_arr)){
                        array_push($show_shangjia_arr,$val['shangjia_id']);
                    }
                }else{
                    unset($course_list[$key]);
                }
            }
            if ($joinid==4 || $grouprice==2){
                $course_min_num = $setting['p2_course_min_num'];
                $course_max_num = $setting['p2_course_max_num'];
            }elseif ($joinid==5 || $grouprice==2){
                $course_min_num = $setting['p3_course_min_num'];
                $course_max_num = $setting['p3_course_max_num'];
            }
        }else{
            //建立前端展示的商家 数组
            $show_shangjia_arr = [];
            foreach ($course_list as $key => $val)
            {
                $course_list[$key]['address'] = json_decode($val['address'],true);
                $course_list[$key]['mappoint'] = json_decode($val['mappoint'],true);
                if (!in_array($val['shangjia_id'],$show_shangjia_arr)){
                    array_push($show_shangjia_arr,$val['shangjia_id']);
                }
            }
        }




        //格式化前端展示商家数组 ['shnagjia_id'=>1,'course_min'=>0,'course_max'=>1]
        $show_shangjia_data = [];
        foreach ($show_shangjia_arr as $key =>$val){
            $item = BeilaiModel::getShangjiaItem($val);
            if ($item){
                $item_other_config = json_decode($item['other_config'],true);
                $tmp_arr = ['shangjia_id'=>(int)$val,'course_min'=>(int)$item_other_config['course_min'],'course_max'=>(int)$item_other_config['course_max']];
                array_push($show_shangjia_data,$tmp_arr);
            }
        }
        $show_shangjia_data = json_encode($show_shangjia_data);

        //格式化前端展示分类数组
        $show_shangjia_category = [];
        $category_list = BeilaiModel::getCategoryListBy(['themeid'=>$themeid]);
        foreach ($category_list as $kb => $vb){
            $tmp_ar = ['ct_id'=>(int)$vb['ct_id'],'ct_sj'=>(int)$vb['ct_sj'],'ct_sj_min'=>(int)$vb['ct_sj_min'],'ct_name'=>$vb['ct_name'],'is_first'=>$vb['pid']==0?1:0];
            array_push($show_shangjia_category,$tmp_ar);
        }

        $show_shangjia_category = json_encode($show_shangjia_category);

        if ($open_shangjia_rules==1){
            //二次遍历 机构类型
            foreach ($temp_shangjia_types_arr as $k1 => $v1){
                foreach ($course_list as $k2 => $v2){
                    if ($v1['id']==$v2['shangjia_rule_id']){
                        $temp_shangjia_types_arr[$k1]['courses'][] = $v2;
                    }
                }
            }
            foreach ($temp_shangjia_types_arr as $kk1 => $vv1){
                if (!isset($vv1['courses'])){
                    $temp_shangjia_types_arr[$kk1]['courses'] = [];
                }
            }
            //二级分类
            foreach ($ct_shangjia_list as $kd => $vd){
                //判断以及分类是否存在二级类
                $child = BeilaiModel::getCategoryListBy(['themeid'=>$themeid,'pid'=>$vd['id']]);
                if ($child){
                    //存在
                    $ct_shangjia_list[$kd]['has_child'] = count($child);
                    foreach ($child as $kd1 => $vd1){
                        $ct_shangjia_list[$kd]['list_info'][$kd1]['title'] = $vd1['ct_name'];
                        $ct_shangjia_list[$kd]['list_info'][$kd1]['firid'] = (int)$vd['id'];
                        foreach ($course_list as $kd2 => $vd2){
                            if ($vd1['ct_id']==$vd2['shangjia_rule_id']){
                                $ct_shangjia_list[$kd]['list_info'][$kd1]['courses'][] = $vd2;
                                $ct_shangjia_list[$kd]['list_info'][$kd1]['secid'] = (int)$vd1['ct_id'];
                            }
                        }
                    }
                }else{
                    //不存在
                    $ct_shangjia_list[$kd]['has_child'] = 1;
                    $tmp_arr_1 = [];
                    foreach ($course_list as $kd2 => $vd2){
                        if ($vd['id']==$vd2['shangjia_rule_id']){
                           array_push($tmp_arr_1,$vd2);
                        }
                    }
                    if (count($tmp_arr_1)<1){
                        $ct_shangjia_list[$kd]['has_child'] = 0;
                    }
                    $tt_arr = ['title'=>$vd['title'],'courses'=>$tmp_arr_1,'firid'=>(int)$vd['id'],'secid'=>0];
                    $ct_shangjia_list[$kd]['list_info'][0] = $tt_arr;
                }
            }
        }

        //通过定位排序  距离由近到远
        if ($user_location){
            foreach ($course_list as $kk => $vv){
                if ($vv['mappoint'] && $vv['address'] && is_array($vv['mappoint'])){
                    $distance_arr = [];
                    foreach ($vv['mappoint'] as $k1 => $v1){
                        $distance_item = BeilaiModel::distance($v1,$user_location);
                        array_push($distance_arr,$distance_item);
                    }
                    $course_list[$kk]['min_distance'] = min($distance_arr);
                }else{
                    $course_list[$kk]['min_distance'] = 0;
                }
            }
            $sort_filed = array_column($course_list,'min_distance');
            if (count($course_list)>0){
                $course_list = Ad::arraySort($course_list,'min_distance','asc');
            }
            if ($open_shangjia_rules==1){
                //二次 遍历机构类型数据
                foreach ($temp_shangjia_types_arr as $kkk => $vvv){
                    $item_temp_courese = $vvv['courses'];
                    foreach ($item_temp_courese as $kv => $vk){
                        if ($vk['mappoint'] && $vk['address'] && is_array($vk['mappoint'])){
                            $distance_arr = [];
                            foreach ($vk['mappoint'] as $k11 => $v11){
                                $distance_item = BeilaiModel::distance($v11,$user_location);
                                array_push($distance_arr,$distance_item);
                            }
                            $item_temp_courese[$kv]['min_distance'] = min($distance_arr);
                        }
                    }
                    $temp_shangjia_types_arr[$kkk]['courses'] = Ad::arraySort($item_temp_courese,'min_distance','asc');
                }

                //二次遍历 两级分类下的机构商家课程
                foreach ($ct_shangjia_list as $ff => $gg){
                    $tmp_gg = $gg['list_info'];
                    if (isset($tmp_gg)){
                        foreach ($tmp_gg as $rr => $tt){
                            if (isset($tt['courses']) && count($tt['courses'])>0){
                                $items_course_tmp = $tt['courses'];
                                foreach ($items_course_tmp as $kv1 => $vk1){
                                    if ($vk1['mappoint'] && $vk1['address'] && is_array($vk1['mappoint'])){
                                        $distance_arr = [];
                                        foreach ($vk1['mappoint'] as $k111 => $v111){
                                            $distance_item = BeilaiModel::distance($v111,$user_location);
                                            array_push($distance_arr,$distance_item);
                                        }
                                        $ct_shangjia_list[$ff]['list_info'][$rr]['courses'][$kv1]['min_distance'] = min($distance_arr);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if ($open_shangjia_rules==1){
            foreach ($temp_shangjia_types_arr as $aa => $bb){
                if (count($bb['courses'])<1){
                    unset($temp_shangjia_types_arr[$aa]);
                }
            }

            foreach ($ct_shangjia_list as $ww => $ee){
                $list_info_tmp = $ee['list_info'];
                if ($ee['has_child']>0){
                    foreach ($list_info_tmp as $bb => $mm){
                        if (!isset($mm['courses'])){
                            $ct_shangjia_list[$ww]['list_info'][$bb]['courses']=[];
                        }
                        if (!isset($mm['secid'])){
                            $ct_shangjia_list[$ww]['list_info'][$bb]['secid']=0;
                        }
                    }
                }
            }
        }

        //针对于分类下的定位距离排序
        if ($user_location && $ct_shangjia_list){
            foreach ($ct_shangjia_list as $k => $v){
                if ($v['list_info']){
                    $tmp_list_info = $v['list_info'];
                    foreach ($tmp_list_info as $k1 => $v1){
                        $tmp_courses = $v1['courses'];
                        if ($tmp_courses){
                            $sort_filed = array_column($tmp_courses,'min_distance');
                            if (count($tmp_courses)>0){
                                array_multisort(array_column($tmp_courses,'min_distance'),SORT_ASC,array_column($tmp_courses,'shangjiaid'),SORT_ASC,$tmp_courses);
                            }
                            $ct_shangjia_list[$k]['list_info'][$k1]['courses'] = $tmp_courses;
                        }
                    }
                }
            }
        }
        //判断机构展示名称
        $company_show_name ='机构';
        if(MODULE_NAME=='sd33855'){
            $company_show_name = '分类';
        }

        //赠品判断
        $free = BeilaiModel::getGiftList($themeid,0,'',0,0,0);
        $gift_total = count($free);

        if ($_S['softstyle']==108 && $_S['beilai_style']==1){
            if ($open_shangjia_rules==1){

                $return_clicent = get_defined_vars();
return $this->view('/beilai/course4', $return_clicent);
            }else{
                $return_clicent = get_defined_vars();
return $this->view('/beilai/course3', $return_clicent);
            }
        }else{
            $return_clicent = get_defined_vars();
return $this->view('/beilai/course', $return_clicent);
        }
    }
//我的奖励
    public function Reword()
    {
        global $_P, $_G,$_S;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $fromuid = $_P['fromuid'];
        $theme = Theme::getThemeById($themeid);
        $_C = $config =    Theme::getConfig($themeid);
        $kefu = ['qr_code'=>$config['kfewm'],'mobile'=>$theme['mobile']];
        //获取我的奖励  假测试数据


        $item = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        $item['tuan'] = json_decode($item['tuan'],true);
        $item['order'] = json_decode($item['order'],true);
        $other_config = json_decode($item['other_config'],true);

        $gift_none_btn_txt = $other_config['gift_none_btn_txt'];
        $gift_has_btn_txt = $other_config['gift_has_btn_txt'];
        $gift_refund_btn_txt = $other_config['gift_refund_btn_txt'];

        $poster_show = $item['poster_show'];
        $temp_arr = [];
        $temp_total_arr = [];
        $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
        if (!$themyrecord){
            //退款判断
            $themyrecord = db_get(t_record,['themeid'=>$themeid,'uid'=>$fans['uid']]);
        }
        if ($item['tuan'] && is_array($item['tuan'])){
            foreach ($item['tuan'] as $key => $val)
            {
                if ($val['key']=='主图'){
                    $temp_arr['img'] = $val['val'];
                }
                if ($val['key']=='提供商家'){
                    $temp_arr['shangjia'] = $val['val'];
                }
                if ($val['key']=='奖品'){
                    $temp_arr['title'] = $val['val'];
                }
                if ($val['key']=='价值'){
                    $temp_arr['price'] = $val['val'];
                }
                if ($val['key']=='奖励描述'){
                    $temp_arr['detail'] = $val['val'];
                }
                if ($val['key']=='id'){
                    $temp_arr['id'] = $val['val'];
                }
                $temp_arr['type'] = 'tuan';
                $temp_arr['from'] = '成团解锁(团长)';
                $temp_arr['invite_desc'] = '成功购买课程后，组团成功后解锁';
            }
            $has_gift =BeilaiModel::checkUserHasGift($themeid,$temp_arr['id'],$fans['uid']);
            if ($themyrecord && $themyrecord['type']==0 && $themyrecord['status']>0 && $themyrecord['isfinish']==1){
                if ($has_gift){
                    $temp_arr['bl_txt'] = '已解锁';
                    $temp_arr['br_txt'] = $gift_has_btn_txt;
                    $temp_arr['user_has'] = true;
                }else{
                    $temp_arr['bl_txt'] = '未解锁';
                    $temp_arr['br_txt'] = '免费获取';
                    $temp_arr['user_has'] = false;
                }
            }elseif ($themyrecord  && $themyrecord['status']==-1){
                if ($has_gift){
                    $temp_arr['bl_txt'] = '退款失效';
                    $temp_arr['br_txt'] = $gift_refund_btn_txt;
                    $temp_arr['user_has'] = true;
                }else{
                    $temp_arr['bl_txt'] = '未解锁';
                    $temp_arr['br_txt'] = '免费获取';
                    $temp_arr['user_has'] = false;
                }
            }else{
                if ($themyrecord['type']!=1){
                    $temp_arr['bl_txt'] = '未解锁';
                    $temp_arr['br_txt'] = $gift_none_btn_txt;
                    $temp_arr['user_has'] = false;
                }
            }
            if (isset($temp_arr['bl_txt']) && $temp_arr['bl_txt']){
                array_push($temp_total_arr,$temp_arr);
            }
        }
        if ($item['order'] && is_array($item['order'])){
            if ($other_config['lock_gift_type']){
                $friend_order_total = (int)BeilaiModel::getFriendOrderTotal($themeid,$fans['uid']);
            }else{
                $friend_order_total = (int)BeilaiModel::getFriendOrderTotal($themeid,$fans['uid'],1,$themyrecord['pid']);
            }

            $setting = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
            $tuan_gift_info = json_decode($setting['tuan'],true);
            if (is_array($tuan_gift_info)){
                foreach ($tuan_gift_info as $key => $val){
                    if ($val['key']=='id'){
                        $tuan_gift_id = $val['val'];
                    }
                }
            }

            //检查用户是否存在记录
            $check_tuan_gift = BeilaiModel::checkUserHasGift($themeid,$tuan_gift_id,$fans['uid']);
            if ($other_config['lock_gift_type'] && $themyrecord && $themyrecord['status']==1){
                $check_tuan_gift=true;
            }
            foreach ($item['order'] as $key => $val){
                $temp_array = [];
                $temp_array['img'] = $val['img'];
                $temp_array['shangjia'] = $val['shangjia_name'];
                $temp_array['title'] = $val['name'];
                $temp_array['price'] = $val['price'];
                $tmp_str = '成团';
                if ($other_config['lock_gift_type']){
                    $tmp_str = '下单';
                }
                $temp_array['from'] = $tmp_str.'后累计邀请'.$val['invite'].'人解锁';
                $temp_array['type'] = 'order';
                $temp_array['detail'] = $val['detail'];
                $temp_array['invite_desc'] = $val['invite_desc'];
                if ($themyrecord['status']==-1){
                    //退款
                    if ($friend_order_total && ($friend_order_total>=(int)$val['invite']) && $check_tuan_gift){
                        $temp_array['bl_txt'] = '退款失效';
                        $temp_array['br_txt'] = $gift_refund_btn_txt;
                        $temp_array['user_has'] = true;
                    }else{
                        $temp_array['bl_txt'] = '未解锁';
                        $temp_array['br_txt'] = $gift_none_btn_txt;
                        $temp_array['user_has'] = false;
                    }
                }else{
                    if ($friend_order_total && ($friend_order_total>=(int)$val['invite']) && $check_tuan_gift){
                        $temp_array['bl_txt'] = '已解锁';
                        $temp_array['br_txt'] = $gift_has_btn_txt;
                        $temp_array['user_has'] = true;
                    }else{
                        if ($friend_order_total && ($friend_order_total>=(int)$val['invite'])){
                            $temp_array['bl_txt'] = '已解锁';
                            $temp_array['br_txt'] = $gift_has_btn_txt;
                            $temp_array['user_has'] = true;
                        }else{
                            $temp_array['bl_txt'] = '未解锁';
                            $temp_array['br_txt'] = $gift_none_btn_txt;
                            $temp_array['user_has'] = false;
                        }
                    }
                }

                array_push($temp_total_arr,$temp_array);
            }
        }
        //赠品list
        $free = BeilaiModel::getGiftList($themeid,0,'',0,0,0);
        $user_free_gifts = BeilaiModel::getFreeFlag($uid,$themeid);
        $free_arr = [];
        if ($user_free_gifts){
            foreach ($user_free_gifts as $key => $val)
            {
                array_push($free_arr,$val['gift_id']);
            }
        }
        if ($free && is_array($free)){

            foreach ($free as $key => $val){
                $temp_arr = [];
                $temp_arr['img'] = $val['top_img'];
                $temp_arr['shangjia'] = $val['shangjia_name'];
                $temp_arr['title'] = $val['name'];
                $temp_arr['price'] = $val['price'];
                $temp_arr['from'] = '购买解锁';
                $temp_arr['type'] = 'free';
                $temp_arr['detail'] = $val['detail'];
                $temp_arr['invite_desc'] = '成功购买课程后解锁';
                $temp_arr['verify'] = false;
                $temp_arr['qrcode'] = '';
                if (is_array($free_arr) && in_array($val['id'],$free_arr)){
                    //判断退款
                    if ($themyrecord  && $themyrecord['status']==-1 ){
                        $temp_arr['bl_txt'] = '退款失效';
                        $temp_arr['br_txt'] = $gift_refund_btn_txt;
                        $temp_arr['verify'] = true;
                        $temp_arr['user_has'] = true;
                    }else{
                        $gift_item = db_get(t_shangjia_user_gift,['themeid'=>$themeid,'uid'=>$uid,'gift_id'=>$val['id']]);
                        $temp_arr['bl_txt'] = '已解锁';
                        if ($gift_item && $gift_item['workerid']){
                            $temp_arr['br_txt'] = '已核销';
                            $temp_arr['verify'] = true;
                        }else{
                            $temp_arr['br_txt'] = $gift_has_btn_txt;
                            $temp_arr['qrcode'] = $_G['siteroot'].createMobileUrl('wx',array('themeid'=>$themeid,'from'=>'mobile','r'=>'beilai.index.Verify','mobile'=>$themyrecord['mobile'],'gift_id'=>$val['id'],'op'=>'display'));
                            $temp_arr['verify'] = false;
                        }
                        $temp_arr['user_has'] = true;
                    }
                }else{
                    $temp_arr['bl_txt'] = '未解锁';
                    $temp_arr['br_txt'] = $gift_none_btn_txt;
                    $temp_arr['user_has'] = false;
                }
                array_unshift($temp_total_arr,$temp_arr);
            }
        }
        $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
        //海报地址

        $poster = Poster::getByThemeIdnew($themeid);
        if(!empty($poster['bg'])){
            $config['isposter']=1;
            if ($fans['status']==2 && $_S['s']['istubiao']==1) {
                $config['isposter']=0;
            }
            $pm = ['themeid'=>$themeid,'openid'=>$fans['openid']];
            $myposter = db_find("SELECT * FROM ".tablename(t_fans)."
         WHERE themeid = :themeid and openid = :openid",$pm);
            if (empty($myposter['poster'])){
                $waittips = empty($poster['waittips'])?'正在生成海报':$poster['waittips'];
            }else{
                $waittips='';
            }
            //获取海报的url地址
            $posterurl = "/" . createMobileUrl("poster",array("themeid"=>$themeid,"op"=>"action","fromuid"=>$_P['fromuid'],"fopenid"=>$_P['fopenid'],"recordid"=>intval($themyrecord['id'])));

        }else{
            $waittips='海报未开启';
        }
        $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid);


        //伪小程序
        if ($_S['pn_wxapp']==3){
            $poster_show==2;
        }

        $canshare =1;
        if ($poster_show==2 && !$themyrecord && !$myworker){
            $posterurl = '';
            $waittips = '成功购买课程后，才能生成海报';
            $canshare=0;
        }
        if ($_S['pn_wxapp']==0 || $_S['pn_wxapp']==1){
            $posterurl = '';
            $waittips = '未开启分享功能';
            $canshare=0;
        }
        $themyrecord_status = $themyrecord['status'];
        $poster_info = ['tips'=>$waittips,'img_url'=>$posterurl];
        //判断活动是否开始
        $start_theme = BeilaiModel::checkThemeStart($theme);
        //判断活动是否结束
        $end_theme = BeilaiModel::checkThemeEnd($theme);
        //核销二维码
        $verify_code = QRURLNEW.urlencode($_G['siteroot'].createMobileUrl('wx',array('themeid'=>$themeid,'from'=>'mobile','r'=>'beilai.index.Verify','mobile'=>$themyrecord['mobile'],'op'=>'display')));
        $return_clicent = get_defined_vars();
return $this->view('/beilai/reword', $return_clicent);
    }
//    我的课程
    public function MineCourse()
    {
        global $_P, $_G,$_S;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $fromuid = $_P['fromuid'];
        $theme = Theme::getThemeById($themeid);
        $_C = $config =    Theme::getConfig($themeid);
        //判断订单状态
        $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
        $kefu = ['qr_code'=>$config['kfewm'],'mobile'=>$theme['mobile']];
        $poster = Poster::getByThemeIdnew($themeid);
        if(!empty($poster['bg'])){
            $config['isposter']=1;
            if ($fans['status']==2 && $_S['s']['istubiao']==1) {
                $config['isposter']=0;
            }
            $pm = ['themeid'=>$themeid,'openid'=>$fans['openid']];
            $myposter = db_find("SELECT * FROM ".tablename(t_fans)."
         WHERE themeid = :themeid and openid = :openid",$pm);
            if (empty($myposter['poster'])){
                $waittips = empty($poster['waittips'])?'正在生成海报':$poster['waittips'];
            }else{
                $waittips='';
            }
            //获取海报的url地址
            $posterurl = "/" . createMobileUrl("poster",array("themeid"=>$themeid,"op"=>"action","fromuid"=>$_P['fromuid'],"fopenid"=>$_P['fopenid'],"recordid"=>intval($themyrecord['id'])));

        }else{
            $waittips='海报未开启';
        }
        $item = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        $poster_show = $item['poster_show'];

        $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid);
        $company_info = [];
        if ($myworker['company']){
            $company_info = BeilaiModel::getShangjiaItem($myworker['company']);
        }
        $setting = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
        $other_config = json_decode($setting['other_config'],true);
        $school_other_name = $other_config['school_other_name'];
        $course_other_name = $other_config['course_other_name'];
        $gift_other_name = $other_config['gift_other_name'];


        $canshare =1;
        if ($poster_show==2 && !$themyrecord && !$myworker){
            $posterurl = '';
            $waittips = '成功购买'.$course_other_name.'后，才能生成海报';
            $canshare=0;
        }
        $poster_info = ['tips'=>$waittips,'img_url'=>$posterurl];

        $user_flag = [];
        if (!$themyrecord){
            //退款判断
            $themyrecord = db_get(t_record,['themeid'=>$themeid,'uid'=>$fans['uid']]);
        }
        if ($themyrecord){
            $user_flag = BeilaiModel::getUserCourseFlag($themeid,0,'',0,0,'','',$themyrecord['id']);
            foreach ($user_flag as $k => $v){
                if ($v['status'] && $v['workerid']){
                    $check_worker = db_get(t_worker,['themeid'=>$themeid,'uid'=>$v['workerid']]);
                    if ($check_worker){
                        $user_flag[$k]['workerid'] = $check_worker['username'];
                        $user_flag[$k]['qrcode'] = ''; //核销二维码

                    }
                }
                $user_flag[$k]['qrcode']  = $_G['siteroot'].createMobileUrl('wx',array('themeid'=>$themeid,'from'=>'mobile','r'=>'beilai.index.Verify','mobile'=>$themyrecord['mobile'],'course_id'=>$v['id'],'op'=>'course'));
            }
        }
        $record_status =1;
        if ($themyrecord['status']==-1){
            $record_status = -1;
        }
        //获取我的选择课程
        $return_clicent = get_defined_vars();
return $this->view('/beilai/mine_course', $return_clicent);
    }
//贝莱核销
    public function Verify()
    {
        global $_P, $_G,$_S;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $_C = $config =    Theme::getConfig($themeid);
        $op = $_P['op']?$_P['op']:'display';
        $theme = Theme::getThemeById($themeid);
        //判断客服
        $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid);
        if (!$myworker){
            if ($_G['ispost']){
                return_json(1, '非员工无法访问','');
            }
            return_msg('您无权访问');
            exit;
        }else{
            if ($op=='display'){
//                订单礼品核销
                //判断礼品核销模式
                $setting = BeilaiModel::getRewardByWhere(['themeid'=>$themeid]);
                $other_config = json_decode($setting['other_config'],true);
                $lock_gift_mode = $other_config['lock_gift_mode'];
                $gift_unlock_min_num = $other_config['gift_unlock_min_num'];
                $gift_unlock_max_num = $other_config['gift_unlock_max_num'];
                if ($myworker['level']<1 || $myworker['level']>3){
                    return_msg('您无权访问');
                    exit;
                }else{
                    $mobile = $_P['mobile'];
                    if (!$mobile){
                        return_msg('手机号绑定的订单不存在');
                        exit;
                    }
                    $record_item = db_get(t_record,['themeid'=>$themeid,'mobile'=>$mobile,'status'=>1]);

                    if (!$record_item){
                        return_msg('该手机号绑定的订单不存在或未支付');
                        exit;
                    }
                    //判断礼品核销模式
                    if ($lock_gift_mode){
                        //判断用户已核销的礼品数
                        $verify_gift_total = BeilaiModel::getUserOrderGiftTotalByWhere($themeid,2,$record_item['id'],1);
                        if ($gift_unlock_min_num>0 && $gift_unlock_max_num>0){
                            if ($verify_gift_total>$gift_unlock_max_num){
                                return_msg('已达到解锁礼品选择数上限');
                                exit;
                            }
                        }else{
                            return_msg('管理端-解锁礼品选择数设置不合法');
                            exit;
                        }
                    }

                    $gift_id = $_P['gift_id'];
                    //判断是否核销
                    $has_verify = $gift_item = db_get(t_shangjia_user_gift,['themeid'=>$themeid,'order_id'=>$record_item['id'],'gift_id'=>$gift_id]);
                    $check_worker = [];
                    //判断课程归属的商家与当前员工的商家是否一致
                    if ($has_verify['shangjiaid']!=$myworker['company'] &&  $myworker['level']!=1){
                        return_msg('您无权核销此礼品，员工不归属于此礼品商家');
                        exit();
                    }
                    if ($has_verify && $has_verify['workerid']){
                        $check_worker = db_get(t_worker,['themeid'=>$themeid,'uid'=>$has_verify['workerid']]);
                    }
                    //获取订单信息 姓名 手机号
                    //获取用户全部的赠品信息
                    $gift_list = BeilaiModel::getUserGift($themeid,$has_verify['shangjiaid'],'',0,50,'','','',$gift_id,0,$record_item['id']);
                }
            }elseif ($op=='course'){
                if ($myworker['level']<1 || $myworker['level']>3){
                    return_msg('您无权访问');
                    exit;
                }
                $flag_id = $_P['flag_id'];
                $mobile = $_P['mobile'];
                if (!$mobile){
                    return_msg('手机号绑定的订单不存在');
                    exit;
                }
                $record_item = db_get(t_record,['themeid'=>$themeid,'mobile'=>$mobile,'status'=>1]);
                if (!$record_item){
                    return_msg('该手机号绑定的订单不存在或未支付');
                    exit;
                }
                if (!$flag_id){
                    return_msg('课程核销记录不存在，请检查后重试');
                    exit();
                }
                $check_worker = [];

                $sql = " SELECT a.id,a.shangjiaid,a.address,a.updated_at,a.status,a.workerid,b.avatar,b.nickname,b.username,b.mobile,c.name,
        c.top_img,d.name as shangjianame,e.fee,e.mobile as order_mobile,f.username as workenname FROM "
                    .tablename(t_shangjia_address_user_course)." AS  a ".
                    " LEFT JOIN ".tablename(t_fans)." AS b ON a.uid=b.uid and b.themeid=$themeid".
                    " LEFT JOIN ".tablename(t_shangjia_course)." AS c ON a.course_id=c.id and c.themeid=$themeid".
                    " LEFT JOIN ".tablename(t_shangjia)." AS d ON a.shangjiaid=d.id ".
                    " LEFT JOIN ".tablename(t_record)." AS e ON a.order_id=e.id and  e.themeid=$themeid".
                    " LEFT JOIN ".tablename(t_worker)." AS f ON e.workerid=f.uid and f.themeid=$themeid".
                    " WHERE a.themeid=$themeid and e.status=1 and a.id=$flag_id";

                $has_verify  = db_find($sql);
                if (!$has_verify){
                    return_msg('用户核销记录不存在，请检查后重试');
                    exit();
                }else{
                    //判断课程归属的商家与当前员工的商家是否一致
                    if ($has_verify['shangjiaid']!=$myworker['company'] &&  $myworker['level']!=1){
                        return_msg('您无权核销此课程，员工不归属于此课程商家');
                        exit();
                    }
                    if ($has_verify['status']==1){
                        $check_worker = db_get(t_worker,['themeid'=>$themeid,'uid'=>$has_verify['workerid']]);
                    }
                }
            } elseif($op=='sure_verify'){
                if ($_G['ispost']){
                    $where = ['id'=>$_P['course_id'],'themeid'=>$themeid,'status'=>0];
                    $data = ['status'=>1,'workerid'=>$myworker['uid']];
                    $res =  BeilaiModel::editUserCourseAddress($where,$data);
                    if (!$res){
                        return_json(1, '课程已核销或课程记录不存在，请检查后重试','');
                    }else{
                        return_json(0, '课程核销成功','');
                    }
                }
            } elseif ($op=='gift_verify'){
                if ($_G['ispost']){
                    $where = ['id'=>$_P['gift_id'],'themeid'=>$themeid,'can_use'=>1];
                    $data = ['workerid'=>$myworker['uid']];
                    $res =  BeilaiModel::updateUserGift($where,$data);
                    if (!$res){
                        return_json(1, '礼品记录不存在，请检查后重试','');
                    }else{
                        return_json(0, '礼品核销成功','');
                    }
                }
            }

        }
        $return_clicent = get_defined_vars();
return $this->view('/beilai/verify', $return_clicent);
    }

    public function getGroupPriceInfo()
    {
        global $_P, $_G,$_S;
        $fans = $this->checkuser();
        $uid = $fans['uid'];
        $themeid = $_P['themeid'];
        $grouprice = $_P['grouprice']?$_P['grouprice']:0;
        $nowgroupneednum = $_P['neednum'];
        $themyrecordid = $_P['myid'];
        $is_finish = false;
        $config = 	Theme::getConfig($themeid);
        $system = $_S;
        if (empty($themyrecordid)){
            if ($nowgroupneednum==0){
                if ($config['maxcountall']>1){
                    if ($config['tuanmode']==0 || $config['tuanmode']==2){
                        $is_finish = true;
                    }
                }
            }
        }
        $recordid = $_P['record'];
        //获取定单信息：theMyRecord
        extract(Record::getTheMyRecord($system,$fans,$recordid,$config,$themeid));

//获取要显示的团明细
        extract(Record::getShowRecord($system,$recordid,$config,$themeid));

        if (empty($themyrecordid) && !empty($showrecord)){
            $txt = "   <div class='mulit_screen'></div>\n";
            $txt .= "   <div class='mulit_teamalt'>\n";

            $txt .= "   <div class='mulit_teamtitle'>\n";
            $txt .= "选择团价格加入";
            $txt .= "   <span class='mulit_closeB'></span>";
            $txt .= "   </div>\n";

            $txt .= " <div class='mulit_peobox'>";
            $txt .= " <ul>";
            $txt .= " <li><img src='". tomedia($showrecord['avatar']) ."'><span class='mulit_tagpeo'>团长</span></li>";
            if (!empty($detaillist)){
                foreach ($detaillist as $cindexno => $item){
                    if ($cindexno==18){
                        $txt .= "           <li><img src=\"".$_G['URL']['YX2020_CSS']."images/useramore.png\"></li>\n";
                    }elseif ($cindexno<18) {
                        $txt .= "                     <li>\n";
                        $txt .= "                         <img src=\"" . tomedia($item['avatar']) . "\">\n";
                        $txt .= "                     </li>\n";
                    }
                }
            }
            if (!empty($num_arr)){
                foreach ($num_arr as $cindexno => $item) {
                    $txt .= "           <li><img src=\"".$_G['URL']['YX2020_CSS']."images/usera.png\"></li>\n";
                }
            }
            $txt .= " </ul>";
            $txt .= " <div class='mulit_relesPeo'>";
            if ($nowgroupneednum!=0){
                $txt .= "          <span>此团仅剩" . $nowgroupneednum . "个名额，您现在付款即可加入此团</span>\n";
            }else{
                if ($config['tuanmode']==0 || $config['tuanmode']==2){
                    $txt .= "          <span>此团已团成功</span>\n";
                }elseif ($config['tuanmode']==1 || $config['tuanmode']==3){
                    $txt .= "          <span>此团已团成功,还可继续加入,共享福利</span>\n";
                }
            }
            $txt .= " </div>";
            $txt .= " </div>";

            $txt .= " <div class='mulit_controlBtn'>";
            if ($system['bottom_jiage']!=0){
                $grouprice1_url = $_G['siteroot'] . createMobileUrl('wx', array('themeid'=>$themeid,'joinid'=>61,'recordid'=>$recordid,'from'=>'mobile','r'=>'beilai.index.course','grouprice'=>1));
                $grouprice2_url = $_G['siteroot'] . createMobileUrl('wx', array('themeid'=>$themeid,'joinid'=>62,'recordid'=>$recordid,'from'=>'mobile','r'=>'beilai.index.course','grouprice'=>2));
                $txt .= "<a href='".$grouprice1_url."'><h2>".$config['groupprice']."</h2><p>立即加入</p></a>";
                $txt .= "<a href='".$grouprice2_url."'><h2>".$config['groupprice2']."</h2><p>立即加入</p></a>";
            }
            if ($system['bottom_jiage']==2){
                $grouprice3_url = $_G['siteroot'] . createMobileUrl('wx', array('themeid'=>$themeid,'joinid'=>63,'recordid'=>$recordid,'from'=>'mobile','r'=>'beilai.index.course','grouprice'=>3));
                $txt .= "<a href='".$grouprice3_url."'><h2>".$config['groupprice3']."</h2><p>立即加入</p></a>";
            }
            $txt .= "   </div>\n";


            $txt .= "   </div>\n";

            $txt .= "   </div>\n";
            return_json(0, 'success',['mulit_price'=>$txt]);

        }
        return_json(1, '暂无数据','');
    }
}